﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMing.Helper.Net40;
using AMing.Extension.Net40;


namespace AMing.Api.Client
{
    public class KeyValueHelper
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public List<Model.KeyValue> List()
        {
            List<Model.KeyValue> list = null;
            try
            {
                HttpHelper httpHelper = new Helper.Net40.HttpHelper();
                string callback = httpHelper.Get(Config.List);

                list = JsonHelper.Deserialize<List<Model.KeyValue>>(callback);
            }
            catch (Exception)
            {

            }

            return list;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Model.KeyValue Get(string key)
        {
            Model.KeyValue data = null;
            try
            {
                string url = Url(Config.Get,
                    new KeyValuePair<string, string>("key", key));

                HttpHelper httpHelper = new Helper.Net40.HttpHelper();
                string callback = httpHelper.Get(url);

                data = JsonHelper.Deserialize<Model.KeyValue>(callback);
            }
            catch (Exception)
            {

            }

            return data;
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Exist(string key)
        {
            bool data = false;
            try
            {
                string url = Url(Config.Exist,
                    new KeyValuePair<string, string>("key", key));

                HttpHelper httpHelper = new Helper.Net40.HttpHelper();
                string callback = httpHelper.Get(url);

                data = callback.To<bool>(false);
            }
            catch (Exception)
            {

            }

            return data;
        }


        /// <summary>
        /// 获取value
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string Value(string key)
        {
            var data = Get(key);

            if (data != null)
            {
                return data.Value;
            }

            return null;
        }

        /// <summary>
        /// 获取value并反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Value<T>(string key)
        {
            string data = Value(key);

            if (string.IsNullOrWhiteSpace(data))
            {
                return default(T);
            }

            return JsonHelper.Deserialize<T>(data);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool Add(Model.KeyValue data)
        {
            try
            {
                string postdata = PostData(
                    new KeyValuePair<string, string>("key", data.Key),
                    new KeyValuePair<string, string>("value", data.Value));

                HttpHelper httpHelper = new Helper.Net40.HttpHelper();
                string callback = httpHelper.Post(Config.Add, postdata);

                return callback.To<bool>(false);
            }
            catch (Exception)
            {

            }
            return false;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool Update(Model.KeyValue data)
        {
            try
            {
                string postdata = PostData(
                    new KeyValuePair<string, string>("key", data.Key),
                    new KeyValuePair<string, string>("value", data.Value));

                HttpHelper httpHelper = new Helper.Net40.HttpHelper();
                string callback = httpHelper.Post(Config.Update, postdata);

                return callback.To<bool>(false);
            }
            catch (Exception)
            {

            }
            return false;
        }

        /// <summary>
        /// 添加或修改（key不存在添加，存在修改）
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool AddOrUpdate(Model.KeyValue data)
        {
            try
            {
                string postdata = PostData(
                    new KeyValuePair<string, string>("key", data.Key),
                    new KeyValuePair<string, string>("value", data.Value));

                HttpHelper httpHelper = new Helper.Net40.HttpHelper();
                string callback = httpHelper.Post(Config.AddOrUpdate, postdata);

                return callback.To<bool>(false);
            }
            catch (Exception)
            {

            }
            return false;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool Delete(string key)
        {
            try
            {
                string postdata = PostData(
                    new KeyValuePair<string, string>("key", key));

                HttpHelper httpHelper = new Helper.Net40.HttpHelper();
                string callback = httpHelper.Post(Config.Delete, postdata);

                return callback.To<bool>(false);
            }
            catch (Exception)
            {

            }
            return false;
        }



        /// <summary>
        /// 整合get地址
        /// </summary>
        /// <param name="url"></param>
        /// <param name="par"></param>
        /// <returns></returns>
        private string Url(string url, params KeyValuePair<string, string>[] par)
        {
            if (par == null || par.Length <= 0)
            {
                return url;
            }
            string par_str = "";
            foreach (var item in par)
            {
                par_str += "&{0}={1}".ToFromat(item.Key, item.Value);
            }

            par_str = par_str.TrimStart('&');
            url += url.Contains('?') ? "&" : "?";
            url += par_str;

            return url;
        }
        /// <summary>
        /// 整合post数据
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        private string PostData(params KeyValuePair<string, string>[] par)
        {
            string data = "";
            foreach (var item in par)
            {
                data += "&{0}={1}".ToFromat(item.Key, System.Web.HttpUtility.UrlEncode(item.Value));
            }

            data = data.TrimStart('&');

            return data;
        }
    }
}
